var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkword: "",
    searchtype: [{
        picimg: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yisheng.png?sign=d13163b9e958d543e5065076e94ed61e&t=1591607233",
        names: "医生"
      },
      {
        picimg: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yiyuan.png?sign=75d413bcae7e1092228ace45acc5d024&t=1591607253",
        names: "药品"
      },
      {
        picimg: "https://6a69-jin5201-a4503e-1257013711.tcb.qcloud.la/%E6%96%B0%E7%89%88%E5%8C%BB%E7%96%97icon/yiyaopin.png?sign=4b09909458316c253eafc286ff6b0202&t=1591607272",
        names: "药品"
      },
    ],
    searchArr: [],
    resultif: false,
    // searchtab:['推荐','医生','药品','药品'],
    searchtab: ['推荐', '医生', '药品'],
    searchindex: 0,
    sindex: 0,
    zixun: [],
    problem: [],
    scrollTop: 0,
    searchlists: [

    ],
  },
  hotdetail(e) {
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var typs = e.currentTarget.dataset.typs;
    var state = e.currentTarget.dataset.state
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblemsInfor/publicProblemsInfor?id=' + id + '&title=' + title + '&typs=' + typs + '&state=' + state,
    })
  },
  delsearch() {
    var that = this;
    wx.showModal({
      title: "确认删除全部历史记录？",
      cancelColor: "green",
      success: function (res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/jiansuo.delsearch',
            'data': {
              openid: wx.getStorageSync('openid'),
            },
            success: function () {
              wx.showToast({
                title: '删除成功',
              })
              setTimeout(function () {
                that.getHistorysearch()
              }, 2000)
            }
          })
        }
      }
    })
  },
  lookzixun(e) {
    wx.navigateTo({
      url: '/hyb_yl/tabBar/jibingyufang/jibingyufang?index=1',
    })
  },
  lookask() {
    wx.navigateTo({
      url: '/hyb_yl/twosubpages/pages/publicProblems/publicProblems',
    })
  },
  doctoritem(e) {
    var zid = e.currentTarget.dataset.zid;
    wx.navigateTo({
      url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid=' + zid,
    })
  },

  btdetail(e) {
    var id = e.currentTarget.dataset.id;
    var p_id = e.currentTarget.dataset.p_id;
    var zid = e.currentTarget.dataset.zid;
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/zixunanlixq/zixunanlixq?id=' + id + '&p_id=' + p_id + '&zid=' + zid,
    })
  },

  // 搜索类型
  typesbtn(e) {
    if (e.currentTarget.dataset.dex == "0") {
      wx.navigateTo({
        url: '../searchyisheng/searchyisheng',
      })
    } else if (e.currentTarget.dataset.dex == "1") {
      wx.navigateTo({
        url: '../searchyiyuan/searchyiyuan',
      })
    } else if (e.currentTarget.dataset.dex == "2") {
      wx.navigateTo({
        url: '../searchyaopin/searchyaopin',
      })
    }
  },
  getSearch: function (e) {
    var that = this;
    var keywords = e.detail.value;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_keywordssearch',
      'data': {
        keywords: keywords
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.length > 0) {
          that.setData({
            checkword: keywords,
            resultif: false,
            searchlisttip: res.data
          })
        } else {
          that.setData({
            checkword: keywords,
            resultif: true,
          })
          app.util.request({
            'url': 'entry/wxapp/jiansuo.gentestusersig',
            'data': {
              openid: wx.getStorageSync('openid'),
              checkword: keywords,
              source: 0,
            },
          });
        }

      },
    })

  },
  listitembtn(e) {
    var that = this;
    var checkword = e.currentTarget.dataset.checkword;
    console.log(checkword);
    app.util.request({
      'url': 'entry/wxapp/jiansuo.addsearch',
      'data': {
        openid: wx.getStorageSync('openid'),
        checkword: checkword,
        source: 0,
      },
    })
    that.setData({
      resultif: true,
      checkword: checkword,
    })

  },
  del: function () {
    var searchArr = this.data.searchArr;
    this.setData({
      searchArr: [],
      searchCont: ""
    })
    wx.setStorage({
      key: 'searchArr',
      data: this.data.searchArr,
    })
  },
  searchHistory: function (e) {
    var that = this;
    var checkword = e.currentTarget.dataset.checkword;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.addsearch',
      'data': {
        openid: wx.getStorageSync('openid'),
        checkword: checkword,
        source: 0,
      },
    })
    that.setData({
      resultif: true,
      checkword: checkword,
    })

  },
  searchtabs: function (e) {
    var that = this;
    const index = e.currentTarget.dataset.index;
    that.setData({
      searchindex: index,
      sindex: index,
    })

  },
  swipertab: function (e) {
    var that = this;
    const index = e.detail.current;
    that.setData({
      searchindex: index,
      sindex: index
    })

  },
  scroll: function (e) {
    var self = this;
    setTimeout(function () {
      console.log(e.detail.scrollTop);
      var list = self.data.list;
      if (list[self.data.curListId]) {
        list[self.data.curListId].scrollTop = e.detail.scrollTop;
        self.setData({
          list
        })
      }
    }, 300);
  },
  listitembtns(e) {
    var sid = e.currentTarget.dataset.sid;
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/product/product?fid=&sid=' + sid,
    })
  },

  listitembtninfor(e) {
    wx.navigateTo({
      url: '../yaopindetail/yaopindetail?name=' + e.currentTarget.dataset.name,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = JSON.parse(options.id)
    var checkword = options.checkword
    var duibid = options.duibid
    app.util.request({
      'url': 'entry/wxapp/tijian.yichang',
      'data': {
        title: checkword,
        openid: wx.getStorageSync('openid'),
        tijianid: id,
        duibid: duibid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          zixunlist: res.data.article,
          goods: res.data.goods,
          zhuanjialist: res.data.doc,
          duibid: duibid
        })
      },
    })
  },


  getHotsearch: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_hotsearch',
      'data': {
        source: 0
      },
      success: function (res) {
        that.setData({
          findArr: res.data
        })
      },
    })
  },
  getHistorysearch: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.show_historysearch',
      'data': {
        openid: wx.getStorageSync('openid'),
        source: 0
      },
      success: function (res) {
        that.setData({
          searchArr: res.data
        })
      },
    })
  },


  getSearchlist: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/jiansuo.searchlist',
      'data': {
        openid: wx.getStorageSync('openid'),
        checkword: that.data.checkword,
        source: 0,
        sindex: that.data.sindex,
      },
      success: function (res) {
        if (that.data.sindex == 0) {
          that.setData({
            zixunlist: res.data.zixunlist,
            answerlist: res.data.answerlist
          })
        }
        if (that.data.sindex == 1) {
          that.setData({
            zhuanjialist: res.data.zhuanjialist,
          })
        }
        if (that.data.sindex == 2) {
          that.setData({
            hospitalist: res.data.yiyuanlist,
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})